package com.ajavaer.framework.common.tools;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class StringTools {
    /**
     * , (逗号)
     */
    public static final String COMMA = ",";
    /**
     * : (冒号)
     */
    public static final String COLON = ":";
    /**
     * / (斜杠)
     */
    public static final String VIRGULE = "/";
    /**
     * _ (下划线)
     */
    public static final String UNDERLINE = "_";
    /**
     * UTF-8编码
     */
    public static final Charset UTF8 = Charset.forName("UTF-8");

    public static boolean isBlank(String str) {
        return str == null || str.trim().equals("");
    }

    public static boolean isBlank(String... str) {
        for (String s : str) {
            if (isBlank(s)) {
                return true;
            }
        }
        return false;
    }

    public static String nvl(String value, String defaultValue) {
        if (isBlank(value) || value.equalsIgnoreCase("null")) {
            return defaultValue;
        }
        return value;
    }

    public static String nvl(String value) {
        return nvl(value, "");
    }

    public static String format(String pattern, Object... args) {
        return MessageFormat.format(pattern, args);
    }

    public static boolean isNumber(String text) {
        if (isBlank(text)) {
            return false;
        }
        String regex = "^(\\d+)(\\.\\d+)?$";
        return regexMatch(text, regex);
    }

    public static boolean isIntegerNumber(String text) {
        if (isBlank(text)) {
            return false;
        }
        String regex = "^\\d+$";
        return regexMatch(text, regex);
    }

    public static String join(Object[] array, String join) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            Object a = array[i];
            stringBuilder.append(a);
            if (i != array.length - 1) {
                stringBuilder.append(join);
            }
        }
        return stringBuilder.toString();
    }

    public static boolean regexMatch(String text, String regex) {
        return Pattern.compile(regex).matcher(text).find();
    }

    public static String processNullString(String str) {
        if (str == null || "null".equalsIgnoreCase(str)) {
            str = "";
        }
        return str.trim();
    }

    public static <T> List<T> split(String str, String mark, Class<T> itemClazz) {
        if (StringTools.isBlank(str, mark)) {
            return new ArrayList<>();
        } else {
            String[] split = str.split(mark);
            List<T> result = new ArrayList<>();
            for (String s : split) {
                try {
                    if (itemClazz == Integer.class) {
                        result.add((T) Integer.valueOf(s));
                    }
                    if (itemClazz == Double.class) {
                        result.add((T) Double.valueOf(s));
                    }
                    if (itemClazz == Float.class) {
                        result.add((T) Float.valueOf(s));
                    }
                    if (itemClazz == Boolean.class) {
                        result.add((T) Boolean.valueOf(s));
                    }
                    if (itemClazz == String.class) {
                        result.add((T) s);
                    }
                } catch (Exception e) {
                }
            }
            return result;
        }
    }

    public static <T> T coverTo(String val, T defaultValue) {
        try {
            if (defaultValue instanceof Long) {
                return (T) Long.valueOf(val);
            } else if (defaultValue instanceof Integer) {
                return (T) Integer.valueOf(val);
            } else if (defaultValue instanceof Double) {
                return (T) Double.valueOf(val);
            } else if (defaultValue instanceof Float) {
                return (T) Float.valueOf(val);
            } else if (defaultValue instanceof Boolean) {
                return (T) Boolean.valueOf(val);
            } else {
                return (T) val;
            }
        } catch (Exception e) {
        }
        return defaultValue;
    }

    public static boolean isNotBlank(String source) {
        return !isBlank(source);
    }

    /**
     * 首字母大写
     *
     * @param str
     * @return
     */
    public static String firstLetterToUpper(String str) {
        if (isBlank(str)) {
            return str;
        }
        char[] array = str.toCharArray();
        if (array[0] >= 97 && array[0] <= 122) {
            array[0] -= 32;
        }
        return String.valueOf(array);
    }

    public static InputStream toInputStream(String text) {
        return new ByteArrayInputStream(text.getBytes(Charset.forName("UTF-8")));
    }

    /**
     * 将字节数组转换为十六进制字符串
     *
     * @param byteArray
     * @return
     */
    public static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符串
     *
     * @param mByte
     * @return
     */
    public static String byteToHexStr(byte mByte) {
        char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
                'B', 'C', 'D', 'E', 'F'};
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];

        return new String(tempArr);
    }

    public static void sort(String a[]) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[j].compareTo(a[i]) < 0) {
                    String temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }

    public static String removeUTF8BOM(String s) {
        if (isNotBlank(s) && s.startsWith("\\uFEFF")) {
            s = s.substring(1);
        }
        return s;
    }

    /***
     * 下划线命名转为驼峰命名
     *
     * @param para
     *        下划线命名的字符串
     */

    public static String underlineToHump(String para,Boolean firstUpperCase) {
        StringBuilder result = new StringBuilder();
        String a[] = para.split(UNDERLINE);
        for (String s : a) {
            if (!para.contains(UNDERLINE)) {
                result.append(s);
                continue;
            }
            if (result.length() == 0 && !firstUpperCase) {
                result.append(s.toLowerCase());
            } else {
                result.append(s.substring(0, 1).toUpperCase());
                result.append(s.substring(1).toLowerCase());
            }
        }
        return result.toString();
    }

}
